package com.qxzn.app.page.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qxzn.app.page.R;
import com.qxzn.app.page.R2;
import com.qxzn.app.util.AppUtil;
import com.qxzn.app.util.DisplayUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CustomItemView extends RelativeLayout {

    @BindView(R2.id.rl_root_view)
    RelativeLayout rlRootView;
    @BindView(R2.id.iv_left_view)
    ImageView ivLeftView;
    @BindView(R2.id.tv_left_view)
    TextView tvLeftView;
    @BindView(R2.id.iv_right_view)
    ImageView ivRightView;
    @BindView(R2.id.iv_right_icon)
    ImageView ivRightIcon;
    @BindView(R2.id.rl_right_view)
    RelativeLayout rlRightView;
    @BindView(R2.id.tv_right_view)
    TextView tvRightView;
    @BindView(R2.id.et_right_view)
    EditText etRightView;
    @BindView(R2.id.view_dot)
    View viewDot;
    private int et_right_view_length;

    public CustomItemView(Context context) {
        super(context);
        initView(context, null);
    }

    public CustomItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public CustomItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_item_view, this, true);
        ButterKnife.bind(this, view);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomItemView);
        if (typedArray != null) {
            int item_height = typedArray.getInt(R.styleable.CustomItemView_item_height, 0);

            int iv_left_view_drawable = typedArray.getResourceId(R.styleable.CustomItemView_iv_left_view_drawable, R.mipmap.logo);
            int iv_left_view_visible = typedArray.getInt(R.styleable.CustomItemView_iv_left_view_visible, View.VISIBLE);
            float iv_left_view_width = typedArray.getDimension(R.styleable.CustomItemView_iv_left_view_width, 0);
            float iv_left_view_height = typedArray.getDimension(R.styleable.CustomItemView_iv_left_view_height, 0);

            int iv_right_view_drawable = typedArray.getResourceId(R.styleable.CustomItemView_iv_right_view_drawable, R.mipmap.icon_right);
            int iv_right_view_visible = typedArray.getInt(R.styleable.CustomItemView_iv_right_view_visible, View.VISIBLE);

            int iv_right_icon_drawable = typedArray.getResourceId(R.styleable.CustomItemView_iv_right_icon_drawable, R.mipmap.logo);
            int iv_right_icon_visible = typedArray.getInt(R.styleable.CustomItemView_iv_right_icon_visible, View.GONE);

            String tv_left_view_text = typedArray.getString(R.styleable.CustomItemView_tv_left_view_text);
            int tv_left_view_color = typedArray.getColor(R.styleable.CustomItemView_tv_left_view_color, getResources().getColor(R.color.black_293137));
            int tv_left_view_visible = typedArray.getInt(R.styleable.CustomItemView_tv_left_view_visible, View.VISIBLE);

            String tv_right_view_text = typedArray.getString(R.styleable.CustomItemView_tv_right_view_text);
            int tv_right_view_with = typedArray.getInt(R.styleable.CustomItemView_tv_right_view_with, 0);
            int tv_right_view_color = typedArray.getColor(R.styleable.CustomItemView_tv_right_view_color, getResources().getColor(R.color.gray_A6A6A6));
            int tv_right_view_visible = typedArray.getInt(R.styleable.CustomItemView_tv_right_view_visible, View.VISIBLE);
            int tv_right_view_padding_right = typedArray.getInt(R.styleable.CustomItemView_tv_right_view_padding_right, 0);

            int et_right_view_visible = typedArray.getInt(R.styleable.CustomItemView_et_right_view_visible, View.GONE);
            int et_right_view_with = typedArray.getInt(R.styleable.CustomItemView_et_right_view_with, 0);
            int et_right_view_input_type = typedArray.getInt(R.styleable.CustomItemView_et_right_view_input_type, 0);
            et_right_view_length = typedArray.getInt(R.styleable.CustomItemView_et_right_view_length, 0);
            int et_right_view_gravity = typedArray.getInt(R.styleable.CustomItemView_et_right_view_gravity, 0);
            int et_right_view_type = typedArray.getInt(R.styleable.CustomItemView_et_right_view_type, 0);
            String et_right_view_text_hint = typedArray.getString(R.styleable.CustomItemView_et_right_view_text_hint);

            int view_dot_visible = typedArray.getInt(R.styleable.CustomItemView_view_dot_visible, View.GONE);

            ivLeftView.setImageResource(iv_left_view_drawable);
            ivLeftView.setVisibility(iv_left_view_visible);
            if (iv_left_view_width > 0 && iv_left_view_height > 0) {
                RelativeLayout.LayoutParams ivLeftParams = (LayoutParams) ivLeftView.getLayoutParams();
                ivLeftParams.width = Math.round(iv_left_view_width);
                ivLeftParams.height = Math.round(iv_left_view_height);
                ivLeftView.setLayoutParams(ivLeftParams);
            }

            ivRightView.setImageResource(iv_right_view_drawable);
            ivRightView.setVisibility(iv_right_view_visible);

            ivRightIcon.setImageResource(iv_right_icon_drawable);
            ivRightIcon.setVisibility(iv_right_icon_visible);

            tvLeftView.setText(tv_left_view_text);
            tvLeftView.setTextColor(tv_left_view_color);
            tvLeftView.setVisibility(tv_left_view_visible);

            tvRightView.setText(tv_right_view_text);
            tvRightView.setTextColor(tv_right_view_color);
            tvRightView.setVisibility(tv_right_view_visible);
            etRightView.setVisibility(et_right_view_visible);

            if (!TextUtils.isEmpty(et_right_view_text_hint)) {
                etRightView.setHint(et_right_view_text_hint);
            }

            if (tv_right_view_padding_right != 0) {
                tvLeftView.setPadding(0, 0, DisplayUtil.dip2px(context, tv_right_view_padding_right), 0);
            }

            if (tv_right_view_with == 1) {
                LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tvRightView.setLayoutParams(params);
            }

            if (et_right_view_with == 1) {
                LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                etRightView.setLayoutParams(params);
            }

            if (et_right_view_input_type == 1) {
                etRightView.setInputType(InputType.TYPE_CLASS_NUMBER);
            }

            if (et_right_view_length != 0) {
                setMaxLength(et_right_view_length);
            }

            if (et_right_view_gravity == 1) {
                etRightView.setGravity(Gravity.RIGHT);
            }

            if (item_height != 0) {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dip2px(context, item_height));
                rlRootView.setLayoutParams(params);
            }

            viewDot.setVisibility(view_dot_visible);
        }
    }

    public String getRightText() {
        return tvRightView.getText().toString().trim();
    }

    public void setLeftText(String text) {
        tvLeftView.setText(text);
    }

    public void setRightText(String text) {
        tvRightView.setText(text);
    }

    public String getRightEditText() {
        return etRightView.getText().toString().trim();
    }

    public void setRightEditText(String text) {
        etRightView.setText(text);
        if (!TextUtils.isEmpty(text) && text.length() <= et_right_view_length) {
            etRightView.setSelection(text.length());
        }
    }

    public String getRightEditHintText() {
        return etRightView.getHint().toString().trim();
    }

    public void setMaxLength(int length) {
        AppUtil.showLog("长度：" + length);
        if (length > 0) {
            etRightView.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
        }
    }

    public ImageView getIvRightIcon() {
        return ivRightIcon;
    }

    public ImageView getIvRightView() {
        return ivRightView;
    }

    public TextView getRightTextView() {
        return tvRightView;
    }

    public View getRightDotView() {
        return viewDot;
    }

}
